/*
 * AS3 MultiTouch Client
 *
 * DragBehaviour - Moves the object to the cursor position  
 *
 * Copyright 2008 Artifacts - Fine Software Development
 * http://www.artifacts.de
 * Author: Michael Markowski (m.markowski@artifacts.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/**
 * DragBehaviour - Moves the object to the cursor position 
 * Uses caurina tweener for moving.  
 */
package de.artifacts.multitouch.behaviours
{
	import de.artifacts.multitouch.Cursor;
	import de.artifacts.multitouch.TouchEvent;
	
	import flash.geom.Point;
	
	public class RotateBehaviour extends TwoFingerBehaviour
	{						
		private var initialAngle : Number;
		
		override public function touchedHandler(event : TouchEvent) : void
		{
			super.touchedHandler(event);			
			if (this.touchingCursors.length < 2) return;

			var cursor1 : Cursor = this.touchingCursors[0];
			var cursor2 : Cursor = this.touchingCursors[1];
			initialAngle = getAngle(cursor1.position, cursor2.position) - touchable.wrappee.rotation;
		}
		
		override public function repositionedHandler(event : TouchEvent) : void
		{
			if (this.touchingCursors.length < 2) return;
			var cursor1 : Cursor = this.touchingCursors[0];
			var cursor2 : Cursor = this.touchingCursors[1];
			
  			touchable.wrappee.rotation = getAngle(cursor1.position, cursor2.position) - initialAngle;  			  
		}		
				
		/**
		 * Returns the angle between the perpendicular of p1 and a
		 * line from p1 to p2
		 */  				
		private function getAngle(p1 : Point, p2 : Point) : Number
		{
		   	var px : Number = p1.x - p2.x;
 		   	var py : Number = p1.y - p2.y;
 		 			
    		var radian : Number = Math.atan2(py,px);
    		var rotation : Number = radian*180/Math.PI;
 			return rotation;
		}
	}
}